Skip to content

fix(metrics): fallback to mock writer on file open failure to prevent panic#596

Open
abhaygoudannavar wants to merge 2 commits intourunc-dev:mainfrom
abhaygoudannavar:one/metrics-file
Open

fix(metrics): fallback to mock writer on file open failure to prevent panic#596
abhaygoudannavar wants to merge 2 commits intourunc-dev:mainfrom
abhaygoudannavar:one/metrics-file

Conversation

@abhaygoudannavar
Copy link
Copy Markdown

@abhaygoudannavar abhaygoudannavar commented May 1, 2026

Description

When timestamps are enabled in the urunc configuration (/etc/urunc/config.toml) but the target file path is invalid or the parent directory does not exist, NewZerologMetrics() returns nil. Because the caller assigns this directly without a nil check, any subsequent metrics.Capture() calls result in a nil pointer dereference, crashing urunc silently.

This PR fixes the issue by returning a &mockWriter{} (safe no-op fallback) and logging a warning instead of returning nil on file open failure. This ensures urunc gracefully disables metrics rather than panicking.

Related issues

How was this tested?

  1. Enabled timestamps in /etc/urunc/config.toml with enabled = true and destination set to a non-existent directory (/var/log/urunc/timestamps.log).
  2. Ran sudo nerdctl run --rm --runtime io.containerd.urunc.v2 harbor.nbfc.io/nubificus/urunc/nginx-qemu-unikraft-initrd:latest.
  3. Verified that urunc starts normally, logs a warning about the file, and gracefully disables metrics without crashing.
  4. Ran make unittest to ensure no existing tests were broken.

LLM usage

No usage

Checklist

  • I have read the contribution guide.
  • The linter passes locally (make lint).
  • The e2e tests of at least one tool pass locally (make test_ctr, make test_nerdctl, make test_docker, make test_crictl).
  • If LLMs were used: I have read the llm policy.

When timestamps are enabled but the target file cannot be opened
(e.g. missing directory), NewZerologMetrics returned nil. This
caused a nil pointer dereference on subsequent metrics.Capture() calls.
This fix gracefully degrades to a no-op mockWriter and logs a warning.

Signed-off-by: abhaygoudannavar <abhaysgoudnvr@gmail.com>
Added TestZerologMetricsInvalidFileDoesNotPanic to ensure that if
the metrics log file cannot be opened, it gracefully degrades
instead of causing a nil pointer dereference.

Signed-off-by: abhaygoudannavar <abhaysgoudnvr@gmail.com>
@netlify
Copy link
Copy Markdown

netlify Bot commented May 1, 2026

Deploy Preview for urunc ready!

Name Link
🔨 Latest commit 51ba5c5
🔍 Latest deploy log https://app.netlify.com/projects/urunc/deploys/69f47543d89a6e0008621cb1
😎 Deploy Preview https://deploy-preview-596--urunc.netlify.app
📱 Preview on mobile
Toggle QR Code...

QR Code

Use your smartphone camera to open QR code link.

To edit notification comments on pull requests, go to your Netlify project configuration.

@cmainas
Copy link
Copy Markdown
Contributor

cmainas commented May 5, 2026

Hello @abhaygoudannavar ,

please do not overwrite the PR description. Read the contribution guide for more information.

@abhaygoudannavar
Copy link
Copy Markdown
Author

@cmainas i have updated the PR description as asked for now it can be merged.

Copy link
Copy Markdown
Contributor

@cmainas cmainas left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hello @abhaygoudannavar ,

you have linked a PR instead of the issue in the description. Also, I left a comment for the unit test.

writer := NewZerologMetrics(true, invalidPath, "container-test")

// Before the fix, writer would be nil here.
if writer == nil {
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants